﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;



namespace WindowsGame
{
	public interface ILights
	{
		Vector3 LightVector { get; }
		Vector4 DiffuseLightMtrl { get; }
		Vector4 AmbientLightMtrl { get; }
		Vector4 SpecularLightMtrl { get; }
		float SpecularPower { get; }
	}



	public class LightsHandler : ILights
	{
		Vector3 lightVector = new Vector3(0.0f, 1.0f, 0.0f);

		Vector4 diffuseLightMtrl = new Vector4(0.5f, 0.5f, 0.5f, 1.0f);
		Vector4 ambientLightMtrl = new Vector4(0.5f, 0.5f, 0.5f, 1.0f);
		Vector4 specularLightMtrl = new Vector4(0.5f, 0.5f, 0.5f, 1.0f);
		float specularPower = 8.0f;

		double timer = 0.0;

		public Vector3 LightVector { get { return lightVector; } }
		public Vector4 DiffuseLightMtrl { get { return diffuseLightMtrl; } }
		public Vector4 AmbientLightMtrl { get { return ambientLightMtrl; } }
		public Vector4 SpecularLightMtrl { get { return specularLightMtrl; } }
		public float SpecularPower { get { return specularPower; } }



		public LightsHandler()
		{
			lightVector = new Vector3(1.0f, 1.0f, 0.0f);
			lightVector.Normalize();
		}



		public void Update(GameTime gameTime)
		{
			timer += gameTime.ElapsedGameTime.TotalSeconds;

			float radians = (float)timer * MathHelper.Pi/60.0f;
			float sin = (float)Math.Sin(radians);
			float cos = (float)Math.Cos(radians);

			if(timer >= 120.0) // 2 minutes
				timer = 0.0;
			else if(timer <= 60.0)
			{
				lightVector.Y = 0.9f * sin;

				ambientLightMtrl.X = 0.3f * sin + 0.2f;
				ambientLightMtrl.Y = 0.4f * sin + 0.1f;
				ambientLightMtrl.Z = 0.5f * sin;

				diffuseLightMtrl.X = 0.3f * sin + 0.7f;
				diffuseLightMtrl.Y = 0.9f * sin + 0.1f;
				diffuseLightMtrl.Z = 1.0f * sin;
			}
			else
			{
				lightVector.Y = -0.9f * sin;

				ambientLightMtrl.X = 0.13f * sin + 0.2f;
				ambientLightMtrl.Y = 0.02f * sin + 0.1f;
				ambientLightMtrl.Z = -0.35f * sin;

				diffuseLightMtrl.X = 0.7f * sin + 0.7f;
				diffuseLightMtrl.Y = 0.1f * sin + 0.1f;
				diffuseLightMtrl.Z = 0.1f * sin;
			}

			lightVector.X = cos;
			lightVector.Z = -sin;
			lightVector.Normalize();
		}
	}
}
